package com.bite.judge.manager;


import com.bit.common.redis.service.RedisService;
import com.bite.common.core.constants.CacheConstants;
import com.bite.judge.domain.UserSubmit;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.concurrent.TimeUnit;

@Component
public class UserSubmitCacheManager {

    @Autowired
    private RedisService redisService;

    public void refreshUserSubmit(UserSubmit userSubmit) {
        if (userSubmit == null) return;
        String userSubmitKey = getUserSubmitKey(userSubmit);
        redisService.deleteObject(userSubmitKey); // 因为只保存一个 所以就全删除即可
        redisService.leftPushForList(userSubmitKey, userSubmit);
    }

    private String getUserSubmitKey(UserSubmit userSubmit) {
        return getUserSubmitKey(userSubmit.getUserId(),
                userSubmit.getQuestionId(), userSubmit.getExamId());
    }

    private String getUserSubmitKey(Long userId, Long questionId, Long examId) {
        String result = CacheConstants.USER_SUBMIT_LIST
                + userId + CacheConstants.SEPARATOR + questionId;
        if (examId != null) {
            result = result + CacheConstants.SEPARATOR + examId;
        }
        return result;
    }
}
